import Vue from 'vue';
import {
	RegularArr
} from './config.js'
/* 
	title	String	是	提示的内容，长度与 icon 取值有关。
	icon	String	否	图标，有效值 "success", "loading", "none"
	duration	Number	否	提示的延迟时间，单位毫秒，默认：1500
	image	String	否	自定义图标的本地路径
	mask	Boolean	否	是否显示透明蒙层，防止触摸穿透，默认：false
 */
export const showLoading = (msg, mask) => {
	const _msg = msg || '加载中'
	const _mask = mask || true
	let params = {
		mask: mask,
		title: _msg
	}
	uni.showLoading(params);
}
export const hideLoading = () => {
	uni.hideLoading();
}

export const fomatFloat = (x, pos) => {
	var f = parseFloat(x);
	if (isNaN(f)) {
		return false;
	}
	f = Math.round(x * Math.pow(10, pos)) / Math.pow(10, pos); // pow 幂   
	var s = f.toString();
	var rs = s.indexOf('.');
	if (rs < 0) {
		rs = s.length;
		s += '.';
	}
	while (s.length <= rs + pos) {
		s += '0';
	}
	return s;
}

export const showToast = (msg, icon, duration, mask, image) => {
	const _icon = icon || 'none'
	const _msg = msg || '系统繁忙，请稍后再试！'
	const _duration = duration || 1500
	const _mask = mask || true
	const _image = image || false
	let params = {
		icon: _icon,
		title: _msg,
		duration: _duration,
		mask: _mask
	}
	if (_image) {
		params.image = image;
	}
	uni.showToast(params);
}

export const showModal = (msg, icon, duration, mask, image) => {
	const _icon = icon || 'none'
	const _msg = msg || '系统繁忙，请稍后再试！'
	const _duration = duration || 1500
	const _mask = mask || true
	const _image = image || false
	let params = {
		icon: _icon,
		title: _msg,
		duration: _duration,
		mask: _mask
	}
	if (_image) {
		params.image = image;
	}
	uni.showModal({
		title: '提示',
		content: params.title,
		success: function (res) {
			if (res.confirm) {
				console.log('用户点击确定');
			} else if (res.cancel) {
				console.log('用户点击取消');
			}
		}
	});

}

// 手机校验
export const CheckPhone = (phone) => {
	let phoneCodeVerification = RegularArr.telReg;
	return phoneCodeVerification.test(phone);
}
// 银行卡校验
export const CheckBankCard = (card) => {
	let cardVerification = RegularArr.bankReg;
	return cardVerification.test(card);
}
export const GetRequest = () => {
	let theRequest = new Object();
	console.log(typeof location)
	if (typeof location == "object") {
		let url = location.search;
		if (url.indexOf("?") === 0) {
			let str = url.substr(1);
			let strs = str.split("&");
			for (let i = 0, len = strs.length; i < len; i++) {
				var curStr = strs[i].split('=');
				curStr[1] && (theRequest[curStr[0]] = curStr[1]); //值不为空才赋值
			}
		}
	} else {
		theRequest = getCurrentPages()[0];
	}
	return theRequest;
}

export const TimeFormat = (number, format) => {
	var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
	var returnArr = [];

	var date = new Date(number);
	returnArr.push(date.getFullYear());
	returnArr.push(formatNumber(date.getMonth() + 1));
	returnArr.push(formatNumber(date.getDate()));

	returnArr.push(formatNumber(date.getHours()));
	returnArr.push(formatNumber(date.getMinutes()));
	returnArr.push(formatNumber(date.getSeconds()));

	for (var i in returnArr) {
		format = format.replace(formateArr[i], returnArr[i]);
	}
	return format;
}

function formatNumber(n) {
	n = n.toString()
	return n[1] ? n : '0' + n
}

export const FillPushTime = ($cur, $ismenu = 1) => {
	let lastArr = [];
	var tmDate = new Date(); // 参数时间戳转换成的日期对象
	var m = tmDate.getMonth() + 1,
		d = tmDate.getDate();
	var nowDate = ('' + m + d) * 1;
	var nowAfter = false; //表示今日之后，日期大小比较，怕跨年，所以加个变量区分下
	// disabled > ismenu  disabled表示不可选，当前选择日期在此之前，ismenu 1表示项目已经被占位了
	$cur.map(item => {
		var curDate = (item.date.split('.').join('')) * 1;
		if (nowDate < curDate) {
			nowAfter = true;
		}
		var curItme = {
			date: item.date,
			week: item.week,
			time: []
		}
		let zh_date = item.time[0].zh_date;
		if (!zh_date) {
			var _date = item.date.split('.') || [];
			zh_date = _date[0] + '月' + _date[1] + '日'
		}
		for (let start = 8, end = 22; start <= end; start++) {
			var curTime = start + ':00';
			curItme.time.push(item.time.find(o => o.time === curTime) || {
				zh_date: zh_date,
				time: curTime,
				ismenu: $ismenu
			})
		}
		curItme.time.forEach(o => {
			o.disabled = !nowAfter && (o.ismenu == 0) ? true : false
		})
		lastArr.push(curItme)
	})
	return lastArr;
}

export const CurrentUrl = () => {
	let currentPages = getCurrentPages();
	let _current = currentPages[currentPages.length - 1] || {};
	let _url = _current.route
	console.log(_url)
	if (_current.options) {
		_url += '?';
		for (var idx in _current.options) {
			_url += (idx + '=' + _current.options[idx] + '&');
		}
		_url = _url.substr(0, _url.length - 1);
	}
	return _url
}

// 返回上一页
export const backPage = () => {
	if (getCurrentPages().length == 1) {
		uni.switchTab({
			url: '../main/main'
		})
	} else {
		uni.navigateBack();
	}
}
// 跳转页面
export const jump = (url) => {
	uni.navigateTo({
		url: url
	})
}
// 将标准时间处理为XXXX-YY
export function handleTime(datetime) {
	// 获取年月日时分秒值  slice(-2)过滤掉大于10日期前面的0
	var year = datetime.getFullYear(),
		month = ("0" + (datetime.getMonth() + 1)).slice(-2),
		date = ("0" + datetime.getDate()).slice(-2),
		hour = ("0" + datetime.getHours()).slice(-2),
		minute = ("0" + datetime.getMinutes()).slice(-2),
		second = ("0" + datetime.getSeconds()).slice(-2);
	// 拼接
	var result = year + "-" + month + "-" + hour
	// 返回
	return result;
}